package com.usian.controller;

import com.github.pagehelper.PageInfo;
import com.usian.appointments.Appointments;
import com.usian.image.Images;
import com.usian.service.AppointmentsService;
import com.usian.user.User;
import com.usian.utils.AliyunOSSUtils;
import com.usian.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/app/appointments")
public class AppointmentsController {
    @Autowired
    private AppointmentsService appointmentsService;
    @Autowired
    private AliyunOSSUtils aliyunOSSUtils;

    @RequestMapping("/findAll")
    public ResultVo findAll(@RequestParam(name = "page", defaultValue = "1")Integer page,
                            @RequestParam(name = "size", defaultValue = "100")Integer size,
                             Appointments appointments){
        PageInfo<Appointments> pageInfo = appointmentsService.findAll(page,size,appointments);
        return ResultVo.ok(pageInfo);
    }

    @RequestMapping("/add")
    public ResultVo add(@RequestBody Appointments appointments){
        appointments.setNum(8);
        appointments.setActiveType("自驾出游");
        int appointments1 = appointmentsService.add(appointments);
        return ResultVo.ok(appointments1);
    }

    @PostMapping("/upload")
    public ResultVo upload(@RequestParam("img") MultipartFile file
    ) throws IOException {
        // 获取文件名
        String filename = file.getOriginalFilename();
        // 获取io流
        InputStream inputStream = file.getInputStream();
        // 文件上传
        String fileUtils = aliyunOSSUtils.putObject(inputStream, filename);
        return ResultVo.ok(fileUtils);
    }

    @RequestMapping("/findById")
    public ResultVo findById(@RequestParam(name = "userId")Integer userId){
        List<Appointments> byId = appointmentsService.findById(userId);

        User user = appointmentsService.selectUserByIdTwo(userId);
        for (Appointments appointments : byId) {
            appointments.setName(user.getName());
            appointments.setImage(user.getImage());
            appointments.setIntroduction(user.getIntroduction());
        }
        return ResultVo.ok(byId);
    }

    @RequestMapping("/echo")
    public ResultVo echo(Integer id){
        Appointments appointments = appointmentsService.echo(id);
        return ResultVo.ok(appointments);
    }






}
